<?php
include "../inc-config.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Delete</title>
<style type="text/css">
<!--
body {
	background-color: #000000;
	background-image: url(../images/alliance-tile.jpg); background-repeat:repeat-x;
}
body,td,th {
	color: #CC9900;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 13px;
}
a:link {
    color:#CC9900; 
	
	
}
a:visited {
    color:#CC9900;
	
}
a:hover {
    color:#ffffff;
}
a:active {
    color:#FF0000;
}	


input { background:transparent; background-image:url(../images/transp.png); color:#FFFFFF; border: 1px solid #333333}
select { background-color:#000000; color:#FFFFFF; border: 1px solid #333333}
textarea { background-color:#000000; color:#FFFFFF; border: 1px solid #333333}

-->
</style></head>

<body><center>
<?php
echo "<br><br><br><br><table style='border: 1px solid #000000; background-image: url(../images/transp.png)'>";
echo "<tr class='headline'><td><center>Delete Post</center></td></tr>";
echo "<tr class='forumrow'><td>";
$user = $_SESSION['user'];
$ID = $_GET['ID'];
$getuser = "SELECT * from b_users a, b_templates b where b.templateid=a.templateclass and a.username='$user'";
$getuser2 = mysql_query($getuser, $forum) or die("Could not get user info");
$getuser3 = mysql_fetch_array($getuser2);

$gettopic = "SELECT * from b_posts, b_users where b_posts.ID='$ID' and b_users.userID=b_posts.author";
$gettopic2 = mysql_query($gettopic, $forum) or die("Could not get topic");
$gettopic3 = mysql_fetch_array($gettopic2);

echo "<link rel='stylesheet' href='templates/$templateclass/style.css' type='text/css'>";

if($getuser3[status] < 1 || ($getuser3['status'] <= $gettopic3['status'] && $getuser3['userID'] != $gettopic3['userID']))
{
	die("<br><br><br><br><table style='border: 1px solid #000000; background-image: url(../images/transp.png)'><tr><td><center>Delete?</center></td></tr><tr class='forumrow'><td><center>You do not have permission to delete this post</center></td></tr></table>");
}
else
{
  if(isset($_POST['submit']))
  {
    $threadid = $_POST['threadid'];
    $ispostreply = "SELECT * from b_posts where ID='$threadid'";
    $isreply2 = mysql_query($ispostreply, $forum) or die("Could not query for post reply");
    $isreply3 = mysql_fetch_array($isreply2);
    if($isreply3['threadparent']!='0')
    {
       $delthread="DELETE from b_posts where ID='$threadid'";
       mysql_query($delthread, $forum) or die("Could not delete thread");
	   
       $upthread="Update b_posts set numreplies=numreplies-1 where ID='$isreply3[threadparent]'";
       mysql_query($upthread, $forum) or die("could not update thread");
	   
       $getlastposter = "SELECT * from b_posts where postforum='$isreply3[postforum]' order by telapsed DESC limit 1";
       $getlastposter2 = mysql_query($getlastposter, $forum) or die(mysql_error());
       $getlastposter3 = mysql_fetch_array($getlastposter2);
       $updateforum = "update b_forums set numposts=numposts-1,lastpost='$getlastposter3[timepost]',lastpostuser='$getlastposter3[lastpost]' where ID='$isreply3[postforum]'";
       mysql_query($updateforum, $forum) or die(mysql_error());
	   
       echo "<center>Post Deleted<br><br><A href='index.php'>Click here to return to main index</a>.</center>";
    }
    else
    {
	   $getuserid = "select author from b_posts where ID='$threadid'";
	   $getuserid2 = mysql_query($getuserid, $forum) or die("Could get user info");
       $getuserid3 = mysql_fetch_array($getuserid2);
	   
	   $getuserinfo = "SELECT honours from b_users where userID='$getuserid3[author]'";
       $getuserinfo2 = mysql_query($getuserinfo, $forum) or die("Could get user info");
       $getuserinfo3 = mysql_fetch_array($getuserinfo2); 
	   
	   $rep = $getuserinfo3['honours'] - 1;
	   $minusrep = "update b_users set honours='$rep' where userID='$isreply3[author]'";
	   $minusrep2 = mysql_query($minusrep, $forum) or die("Could not remove 1 rep");
	   
       $delthread="DELETE from b_posts where ID='$threadid'";
       mysql_query($delthread, $forum) or die("Could not delete thread");
	   
       $getforuminfo="SELECT * from b_posts where postforum='$isreply3[postforum]'";
       $getforuminfo2=mysql_query($getforuminfo, $forum) or die("Could get forum info");
       $getforuminfo3=mysql_num_rows($getforuminfo2);     
	   
       $delrest="DELETE from b_posts where threadparent='$threadid'";
       mysql_query($delrest, $forum) or die("Could not delete sub posts");
	   
       $getlastposter="SELECT * from b_posts where postforum='$isreply3[postforum]' and threadparent='0' order by telapsed DESC limit 1";
       $getlastposter2=mysql_query($getlastposter, $forum) or die("Could not get last poster");
       $getlastposter3=mysql_fetch_array($getlastposter2);
	   
       $updateforum="update b_forums set numtopics=numtopics-1, numposts='$getforuminfo3',lastpost='$getlastposter3[timepost]', lastpostuser='$getlastposter3[lastpost]' where ID='$isreply3[postforum]'";
       mysql_query($updateforum, $forum) or die("Could not update forums");
	   
       echo "<center>Thread Deleted, thread poster loose 1 rep. Now have $rep rep.<br><br><A href='index.php'>Click here to return to main index</a>.</center>";
  }
 }
  else
  {
   
    echo "<form action='deletepost.php?ID=$ID' method='post'>";
    echo "Are You sure you want to delete this post?<br>";
    echo "<input type='hidden' name='threadid' value='$ID'>";
    echo "<br><center><input type='submit' name='submit' value='Delete Post'></center></form>";  
  }
}
?>
</center>
</body>
</html>